package org.unisinos.gc.util;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.struts.upload.FormFile;
import org.unisinos.gc.vo.Artigo;

public class FileUtil {

	public static void copyFile(FormFile fromFile) throws FileNotFoundException,
			IOException {
		InputStream from = null;
		FileOutputStream to = null;
		try {
			from = fromFile.getInputStream();

			to = new FileOutputStream(Artigo.DIR + fromFile.getFileName());
			byte[] buffer = new byte[4096];
			int bytesRead;

			while ((bytesRead = from.read(buffer)) != -1) {
				to.write(buffer, 0, bytesRead);
			}
		} finally {
			if (from != null)
				try {
					from.close();
				} catch (IOException e) {
				}
				if (to != null)
					try {
						to.flush();
						to.close();
					} catch (IOException e) {
					}
		}
	}
}

